package com.orange.common.util

import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

object TimeUtils {
    /**
     * 获取时区偏移量
     */
    @JvmStatic
    fun getTimeZoneOffset(): Int {
        val calendar: Calendar = Calendar.getInstance()
        return (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / 1000
    }

    /**
     * 获取时区代码
     * http://www.timezoneconverter.com/cgi-bin/zonehelp
     */
    @JvmStatic
    fun getTimeZone(): String? {
        return Calendar.getInstance().timeZone.id
    }

    /**
     * 时间格式化
     */
    fun dateFormat(date: Date = Date(), format: String = "yyyy-MM-dd"): String {
        val pattern = SimpleDateFormat(format, Locale.getDefault())
        return pattern.format(date)
    }

    /**
     * 秒 -> 分:秒
     * 10 -> 00:10
     */
    fun second2MS(length: Long): String {
        var minute = length / 60L
        val second = length % 60L
        return String.format("%02d", minute) + ":" + String.format("%02d", second)
    }

    /**
     * 秒 -> 时:分:秒
     * 10 -> 00:00:10
     */
    fun second2HMS(length: Long): String {
        var minute = length / 60L
        val hour = minute / 60L
        val second = length % 60L
        minute %= 60L
        return if (hour == 0L) {
            String.format("%02d:%02d", minute, second)
        } else String.format("%02d:%02d:%02d", hour, minute, second)
    }
}